<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - loaders - Babylon loader</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #000;
				color: #fff;
				margin: 0px;
				overflow: hidden;
			}
			#info {
				color: #fff;
				position: absolute;
				top: 10px;
				width: 100%;
				text-align: center;
				z-index: 100;
				display:block;
			}
			#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
		</style>
	</head>

	<body>
		<div id="info">
		<a href="http://threejs.org" target="_blank">three.js</a> - BabylonLoader test
		</div>

		<script src="../build/three.js"></script>
		<script src="js/loaders/BabylonLoader.js"></script>
		<script src="js/controls/TrackballControls.js"></script>

		<script>

			var camera, controls, scene, renderer;

			init();

			function init() {

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.z = 100;

				controls = new THREE.TrackballControls( camera );

				// scene

				scene = new THREE.Scene();

				// texture

				var manager = new THREE.LoadingManager();
				manager.onProgress = function ( item, loaded, total ) {

					console.log( item, loaded, total );

				};

				var texture = new THREE.Texture();

				var material = new THREE.MeshBasicMaterial( { color: 'red' } );

				var onProgress = function ( xhr ) {
					if ( xhr.lengthComputable ) {
						var percentComplete = xhr.loaded / xhr.total * 100;
						console.log( Math.round(percentComplete, 2) + '% downloaded' );
					}
				};

				var onError = function ( xhr ) {
				};

				// model

				var loader = new THREE.BabylonLoader( manager );
				loader.load( 'models/babylon/skull.babylon', function ( babylonScene ) {

					babylonScene.traverse( function ( object ) {

						if ( object instanceof THREE.Mesh ) {

							object.material = new THREE.MeshPhongMaterial( {
								color: Math.random() * 0xffffff
							} );

						}

					} );

					scene = babylonScene;

					animate();

				}, onProgress, onError );

				//

				renderer = new THREE.WebGLRenderer();
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				document.body.appendChild( renderer.domElement );

				//

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

				controls.handleResize();

			}

			//

			function animate() {

				requestAnimationFrame( animate );
				render();

			}

			function render() {

				controls.update();
				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
